﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;

namespace ControlLibrary
{
    public class StoryControl : ItemsControl
    {
        DispatcherTimer _timer = new DispatcherTimer();
        ListBox _selectionBox;
        public StoryControl()
        {
            this.DefaultStyleKey = typeof(StoryControl);
            Loaded += new RoutedEventHandler(StoryControl_Loaded);
            Unloaded += new RoutedEventHandler(StoryControl_Unloaded);

            _timer.Tick += new EventHandler(_timer_Tick);
            _timer.Interval = new TimeSpan(0,0,0,10,0);
        }

        void StoryControl_Unloaded(object sender, RoutedEventArgs e)
        {
            _timer.Stop();
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            nextButton_Click(null, null);
        }

        void StoryControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (_selectionBox != null)
            {
                if (Items.Count > 0)
                {
                    _selectionBox.SelectedIndex = -1;
                    _selectionBox.SelectedIndex = 0;
                }

                _timer.Start();
            }
        }


        public static readonly DependencyProperty SelectedIndexProperty = DependencyProperty.Register("SelectedIndex", typeof(int), typeof(StoryControl), new PropertyMetadata(0));
		public int SelectedIndex
		{
    		get { return (int) base.GetValue(SelectedIndexProperty); }
    		set { base.SetValue(SelectedIndexProperty, value); }
		}

        public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(StoryControl), new PropertyMetadata(null));
		public object SelectedItem
		{
    		get { return base.GetValue(SelectedItemProperty); }
    		set { base.SetValue(SelectedItemProperty, value); }
		}

        public static readonly DependencyProperty TransitionStyleProperty = DependencyProperty.Register("TransitionStyle", typeof(Style), typeof(StoryControl), new PropertyMetadata(null));
		public Style TransitionStyle
		{
			get { return (Style)GetValue(TransitionStyleProperty); }
			set { SetValue(TransitionStyleProperty, value); }
		}

        public static readonly DependencyProperty ReversedProperty = DependencyProperty.Register("Reversed", typeof(bool), typeof(StoryControl), new PropertyMetadata(false));
		public bool Reversed
		{
			get { return (bool)GetValue(ReversedProperty); }
			set { SetValue(ReversedProperty, value); }
		}

        public static readonly DependencyProperty InCarouselModeProperty = DependencyProperty.Register("InCarouselMode", typeof(bool), typeof(StoryControl), new PropertyMetadata(true));
		public bool InCarouselMode
		{
			get { return (bool)GetValue(InCarouselModeProperty); }
			set { SetValue(InCarouselModeProperty, value); }
		}
		
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			
			Button prevButton = this.GetTemplateChild("PreviousButton") as Button;
			if (prevButton != null)
			{
				prevButton.Click += new System.Windows.RoutedEventHandler(prevButton_Click);
			}

			Button nextButton = this.GetTemplateChild("NextButton") as Button;
			if (nextButton != null)
			{
				nextButton.Click += new System.Windows.RoutedEventHandler(nextButton_Click);
			}

            _selectionBox = this.GetTemplateChild("SelectionBox") as ListBox;
            if (_selectionBox != null)
            {
                _selectionBox.SelectionChanged += new SelectionChangedEventHandler(selectionBox_SelectionChanged);

            }
		}

        void selectionBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SelectedIndex = _selectionBox.SelectedIndex;
            SelectedItem = _selectionBox.SelectedItem;
        }

        protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);

            if (this.Items.Count > 0 & _selectionBox != null)
            {
                _selectionBox.ItemsSource = this.Items;
                _selectionBox.SelectedItem = this.Items[this.SelectedIndex];
                SelectedItem = this.Items[this.SelectedIndex];
                selectionBox_SelectionChanged(null, null);
            }
        }

		protected void prevButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            try
            {
                this.Reversed = this.InCarouselMode;
                _selectionBox.SelectedIndex = (this.SelectedIndex - 1 + this.Items.Count) % (this.Items.Count);
                _selectionBox.SelectedItem = this.Items[this.SelectedIndex];
            }
            catch (DivideByZeroException) { }
		}

		protected void nextButton_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            try
            {
                this.Reversed = false;
                _selectionBox.SelectedIndex = (this.SelectedIndex + 1) % (this.Items.Count);
                _selectionBox.SelectedItem = this.Items[this.SelectedIndex];
            }
            catch (DivideByZeroException) { }
		}

    }
}
